package com.why.controller;

import com.why.pojo.User;
import com.why.service.UserService;
import com.why.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
public class loginController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    //注册
    @RequestMapping("/regist")
    public String regist(User user) {
        user.setPassword(SecurityUtils.md5Hex3(user.getPassword()));
        if (userService.insertSelective(user)) {
            return "success";
        }
        return "failed";
    }

    //登录
    @RequestMapping("/login")
    public String login(User user, HttpServletResponse response) {
        String password = SecurityUtils.md5Hex3(user.getPassword());
        User redisUser = userService.findUserByName(user);
        if (redisUser != null) {
            if (password.equals(redisUser.getPassword())) {
                HashMap<String, Object> map = new HashMap<>();
                String id = UUID.randomUUID().toString();
                Cookie cookie = new Cookie("cookieUser", id);
                cookie.setMaxAge(60*30);
                cookie.setPath("/");
                response.addCookie(cookie);
                map.put("user", redisUser);
                map.put("time",new Date());
                redisTemplate.opsForValue().set(id, map, 30, TimeUnit.MINUTES);

                return "success";
            }
        }else {
            return "error";
        }
        return "failed";
    }

    //查询是否登录
    @RequestMapping(value = "/logined", produces = "application/json;charset=utf-8")
    public HashMap<String, Object> logined(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (null == cookies) {
            return null;
        } else {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("cookieUser")) {
                    HashMap<String, Object> map = (HashMap<String, Object>) redisTemplate.opsForValue().get(cookie.getValue());
                    User user = (User) map.get("user");
                    if (null != user) {
                        return map;
                    }
                }
            }
        }
        return null;
    }

    //注销
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        if (null == cookies) {
            return null;
        } else {
            for (Cookie cookie : cookies) {
                if (redisTemplate.delete(cookie.getValue())) {
                    if (cookie.getName().equals("cookieUser")) {
                        cookie.setValue(null);
                        cookie.setMaxAge(0);// 立即销毁cookie
                        cookie.setPath("/");
                        System.out.println("被删除的cookie名字为:" + cookie.getName());
                        response.addCookie(cookie);
                        return "success";
                    }
                }
            }
        }
        return "failed";
    }

    //判断用户名是否唯一
    @RequestMapping("/loginNameExist")
    public String loginNameExist(String loginname) {
        User user = new User();
        user.setLoginname(loginname);
        if (userService.findUserByName(user) != null) {
            return "failed";
        }
        return "success";
    }

}
